package com.cljz.makepassenger.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.NearBySearchP;
import com.cljz.makepassenger.utils.date.TextUtil;
import com.cljz.makepassenger.widget.ClearEditText;
import com.cljz.makepassenger.widget.FlagButton;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/28.
 */

public class NearBySearchActivity extends BaseActivity<NearBySearchP> implements FlagButton.OnFlagItemClick {
    @BindView(R.id.cet_nearby_search)
    ClearEditText cetNearbySearch;
    @BindView(R.id.btn_nearby_submit)
    Button btnNearbySubmit;
    @BindView(R.id.fb_search_history)
    FlagButton fbSearchHistory;
    private List<String> historyList;

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String key = bundle.getString("key");
        if (key != null && key.length() > 0) {
            cetNearbySearch.setText(key);
            cetNearbySearch.setSelection(key.length());
        }
        getP().getSearchHistory();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nearby_search;
    }

    @Override
    public NearBySearchP newP() {
        return new NearBySearchP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxTextView.textChanges(cetNearbySearch)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 0) {
                            btnNearbySubmit.setText("取消");
                        } else {
                            btnNearbySubmit.setText("搜索");
                        }
                    }
                });
        RxView.clicks(btnNearbySubmit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String key = cetNearbySearch.getText().toString().trim();
                        if (TextUtil.isEmpty(key)) {
                            Router.pop(NearBySearchActivity.this);
                        } else {
                            getP().setSearchHistory(key);
                            return2Nearby(key);
                        }
                    }
                });
    }

    public void return2Nearby(String key) {
        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        Router.pop(NearBySearchActivity.this);
    }

    @Override
    public void OnItemClick(int position) {
        return2Nearby(historyList.get(position));
    }

    public void showHistory(List<String> history) {
        historyList = new ArrayList<>();
        if (history != null) {
            int size = history.size();
            int position = size - 1;
            if (size < 8) {
                for (int i = position; i >= 0; i--) {
                    historyList.add(history.get(i));

                }
            } else {
                for (int i = position; i >= size - 8; i--) {
                    historyList.add(history.get(i));
                }
            }
            int[] margin = new int[]{0, 30, 0, 30};
            fbSearchHistory.initChildViews(historyList, R.layout.item_search_history, margin, this);
        }
    }
}
